class Solution
{
    int row, col;
    bool check[101][101];
    int dx[4] = { 1,-1,0,0 };
    int dy[4] = { 0,0,1,-1 };
public:
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance)
    {
        //BFS
        int ret = 0;
        row = maze.size(), col = maze[0].size();
        int sr = entrance[0], sc = entrance[1];
        BFS(maze, sr, sc, ret);
        return ret;
    }
    void BFS(vector<vector<char>>& maze, int i, int j, int& ret)
    {
        queue<pair<int, int>> q1;
        q1.push({ i,j });
        check[i][j] = true;
        while (q1.size())
        {
            ++ret;
            int sz = q1.size();
            for (int i = 0; i < sz; i++)
            {
                auto [a, b] = q1.front();
                q1.pop();
                for (int k = 0; k < 4; k++)
                {
                    int x = a + dx[k], y = b + dy[k];
                    if (x >= 0 && x < row && y >= 0 && y < col && !check[x][y] && maze[x][y] == '.')
                    {
                        if (x == 0 || x == row - 1 || y == 0 || y == col - 1) return;
                        q1.push({ x,y });
                        check[x][y] = true;
                    }
                }
            }
        }
        ret = -1;
    }
};